/**
 * 
 */
define(function(require) {
    var comm = require('sdk/server');
    require('sdk/common');
    var commentid = appcan.ls.val('crossParam');
    if (!commentid) {
        return alert('缺少参数');
    }
    //评星
    require('raty');

    var scoreDataTemp = $('#commentScoreTemp').val();
    var scoreDataRender = function(data, getMore) {
        // alert(data.data);
        var render = etpl.compile(scoreDataTemp);

        var scoreList = data.data[0];
        var html = render({
            data: scoreList
        });

        if (getMore) {
            $('#commentScore').append(html);
        } else {
            appcan.loading.hide();
            $('#commentScore').html(html);
        }
        $('.total_raty ._score').each(function(i, e) {
            var score = parseFloat($(e).text());
            $(e).parent().find('._stars').raty({
                score: score,
                readOnly: true
            });
        });
    }
    var dataTemp = $('#commentListTemp').val();
    var dataRender = function(data, getMore) {

        var render = etpl.compile(dataTemp);

        console.log(data)
        var html = render(data);

        if (getMore) {
            $('#commentList').append(html);
        } else {
            appcan.loading.hide();
            $('#commentList').html(html);
        }
        $('#mainCont').cacheImg().scrollLoad(function() {
            getData(true);
        });
    }

    var getData = function(getMore) {
        getPartScoreData();
        getPartCommentData();
    }

    var getPartScoreData = function(getMore) {
        appcan.toload({
            url: appcfg.host.control + '/goods/getGoodsEvaluate.jsp',
            reload: getMore,
            data: {
                goods_id: commentid
            },
            success: function(res) {
                if (res.status === 'Y') {
                    scoreDataRender(res, getMore);
                } else {
                    appcan.loading.hide();
                    $('#artDetail').empty();
                    var html = comm.commonTemp('listPlaceholder', {
                        icon: '&#xe7ce;',
                        text: '暂时没有评价哦~'
                    });
                    $('#mainCont').html(html);
                }
            },
            nomore: function() {}
        });
    }
    var getPartCommentData = function(getMore) {
        appcan.toload({
            url: appcfg.host.control + '/goods/getCommentList.jsp',
            reload: getMore,
            size: 10,
            data: {
                goods_id: commentid
            },
            success: function(res) {
                if (res.status === 'Y') {
                    dataRender(res, getMore);
                } else {
                    appcan.loading.hide();
                    $('#commentList').empty();
                    console.log(res)
                }
            },
            nomore: function() {
                $('#commentList').append('<div class="btn btn-block btn-link">没有更多了</div>');
                $('#mainCont').scrollLoad(false);
            }
        });
    }

    appcan.ready(function() {
        getData();

    });
});